﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Soft.Xml
{

    /// <summary>
    /// ClassConfig的集合
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class BaseConfigCollection<T> : BaseSubElement, IEnumerable<T>, IEnumerable where T : BaseSubElement
    {

        public BaseConfigCollection(XmlElement current) : base(current)
        {
        }


        public int Count
        {
            get
            {
                return base.CurrentElement.SelectNodes(typeof(T).Name).Count;
            }
        }


        /// <summary>
        /// 是否是只读的
        /// </summary>
        public bool IsReadOnly { get; protected set; } = true;


        /// <summary>
        ///  创建config配置对象
        /// </summary>
        /// <returns></returns>
        public T CreateConfig()
        {
            XmlElement xmlElement = base.CurrentElement.OwnerDocument.CreateElement(typeof(T).Name);
            base.CurrentElement.AppendChild(xmlElement);
            return (T)((object)Activator.CreateInstance(typeof(T), new object[]
            {
                xmlElement
            }));
        }


        /// <summary>
        /// 克隆节点
        /// </summary>
        /// <param name="node"></param>
        /// <param name="deep"></param>
        /// <returns></returns>
        public T CloneAndAdd(T node, bool deep)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            XmlNode xmlNode = node.CurrentElement.CloneNode(deep);
            base.CurrentElement.AppendChild(xmlNode);
            return (T)((object)Activator.CreateInstance(typeof(T), new object[]
            {
                xmlNode
            }));
        }


        /// <summary>
        /// 清空
        /// </summary>
        public void Clear()
        {
            base.CurrentElement.InnerXml = "";
        }


        /// <summary>
        /// 移除
        /// </summary>
        /// <param name="item"></param>
        public void Remove(T item)
        {
            base.CurrentElement.RemoveChild(item.CurrentElement);
        }


        /// <summary>
        /// 迭代器
        /// </summary>
        /// <returns></returns>
        public IEnumerator<T> GetEnumerator()
        {
            foreach (object obj in base.CurrentElement.SelectNodes(typeof(T).Name))
            {
                XmlNode xmlNode = (XmlNode)obj;
                yield return (T)((object)Activator.CreateInstance(typeof(T), new object[]
                {
                    (XmlElement)xmlNode
                }));
            }
            IEnumerator enumerator = null;
            yield break;
            yield break;
        }


        /// <summary>
        /// 迭代器
        /// </summary>
        /// <returns></returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }


    }
}
